package gew.system;

import com.google.common.base.Strings;
import gew.services.LocaleService;
import gew.utils.SqlUtils;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter(filterName = "UrlRewriteFilter", urlPatterns = {"/*"})
public class UrlRewriteFilter implements javax.servlet.Filter {

    private final static String INDEX = "index.html";
    private final static String ROBOTS = "robots.txt";
    private final static String FAVICON = "favicon.ico";
    private final static String ERROR_404 = "error404.html";
    private final static String HEADER_USER_AGENT = "user-agent";
    private final static String HEADER_USER_AGENT_MOBILE_KEY_WORD = "MOBILE";
    private final static String HEADER_EXPIRES = "Expires";
    private final static String HEADER_EXPIRES_VALUE;
    private final static String HEADER_VARY = "Vary";
    private final static String HEADER_VARY_VALUE_ACCEPT_ENCODING = "Accept-Encoding";
    private final static String HEADER_CACHE_CONTROL = "Cache-Control";
    private final static String HEADER_CACHE_CONTROL_VALUE_PUBLIC = "public";

    static {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 3);
        HEADER_EXPIRES_VALUE = DateFormat.getDateInstance(DateFormat.MEDIUM).format(cal.getTime());
    }

    private final static Integer SECTION_LOCALE = 0;
    private final static Integer SECTION_SECTION = 1;

    private final static Integer SECTION_TRADE_REPORTER = 2;
    private final static Integer SECTION_TRADE_FLOW = 3;
    private final static Integer SECTION_TRADE_PARTNER = 4;
    private final static Integer SECTION_TRADE_PERIOD = 5;
    private final static Integer SECTION_TRADE_COMMODITY_GROUP_TYPE = 6;
    private final static Integer SECTION_TRADE_COMMODITY = 7;
    private final static Integer SECTION_TRADE_PARTNER_RANK = 8;

    private final static Integer SECTION_TRADE_SUMMARY_CLASSIFICATION = 2;
    private final static Integer SECTION_TRADE_SUMMARY_REPORTER = 3;

    private final static Integer SECTION_INDICATOR_INDICATOR = 2;
    private final static Integer SECTION_INDICATOR_COUNTRY = 3;

    private final static Integer SECTION_INDUSTRIES_INDICATOR = 2;
    private final static Integer SECTION_INDUSTRIES_COUNTRY = 3;

    private final static Integer SECTION_TREE2_REQUEST_TYPE = 2;
    private final static Integer SECTION_TREE2_TEMPLATE_TYPE = 3;
    private final static Integer SECTION_TREE2_FILTER_CODE = 4;
    private final static Integer SECTION_TREE2_URL_TEMPLATE = 5;
    private final static Integer SECTION_TREE2_SELECTED_IDS = 6;
    private final static Integer SECTION_TREE2_FILTER_TEXT = 7;

    private final static Integer SECTION_TRADE_TABLE_REPORTER = 2;
    private final static Integer SECTION_TRADE_TABLE_FLOW = 3;
    private final static Integer SECTION_TRADE_TABLE_PARTNER = 4;
    private final static Integer SECTION_TRADE_TABLE_PERIOD = 5;
    private final static Integer SECTION_TRADE_TABLE_COMMODITY_GROUP_TYPE = 6;
    private final static Integer SECTION_TRADE_TABLE_COMMODITY = 7;
    private final static Integer SECTION_TRADE_TABLE_LOAD_TABLE_DATA = 8;

    private final static Integer SECTION_SEARCH_FILTER_TEXT = 2;
    private final static Integer SECTION_SEARCH_SECTION = 3;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        String realURI = null;
        String requestURI = httpRequest.getRequestURI();
        String[] nodes = requestURI
                .replace(httpRequest.getContextPath(), SqlUtils.EMPTY)
                .substring(1).split(SqlUtils.SLASH);

//        String userAgent = httpRequest.getHeader(HEADER_USER_AGENT);
        boolean mobile = false;
//        if (!Strings.isNullOrEmpty(userAgent)) {
//            userAgent = userAgent.toUpperCase();
//            mobile = userAgent.contains(HEADER_USER_AGENT_MOBILE_KEY_WORD);
//        }

        if (nodes.length > 0) {
            switch (nodes[0]) {
                case INDEX:
                case SqlUtils.EMPTY: {
                    realURI = getHomeUri(nodes, requestURI, mobile);
                }
                break;
                case ROBOTS:
                case FAVICON:
                case ERROR_404: {
                    realURI = null;
                }
                case Constants.SECTION_CONST: {
                }
                break;
                case Constants.SECTION_RESOURCES: {
                    HttpServletResponse httpResponse = (HttpServletResponse) response;
                    if (!httpResponse.containsHeader(HEADER_EXPIRES)) {
                        httpResponse.addHeader(HEADER_EXPIRES, HEADER_EXPIRES_VALUE);
                    }
                    if (nodes.length > 1) {
                        switch (nodes[1]) {
                            case Constants.SECTION_FA:
                            case Constants.SECTION_JS:
                            case Constants.SECTION_CSS: {
//                                if (!httpResponse.containsHeader(HEADER_VARY)) {
//                                    httpResponse.addHeader(HEADER_VARY, HEADER_VARY_VALUE_ACCEPT_ENCODING);
//                                }
                            }
                            case Constants.SECTION_IMAGES: {
                                if (!httpResponse.containsHeader(HEADER_CACHE_CONTROL)) {
                                    httpResponse.addHeader(HEADER_CACHE_CONTROL, HEADER_CACHE_CONTROL_VALUE_PUBLIC);
                                }
                            }
                            break;
                        }
                    }
                }
                break;
                case LocaleService.EN:
                case LocaleService.RU: {
                    if (nodes.length > 1) {
                        switch (nodes[1]) {
                            case Constants.SECTION_ABOUT: {
                                realURI = getAboutUri(nodes);
                            }
                            break;
                            case Constants.SECTION_ERROR: {
                                realURI = getErrorUri(nodes);
                            }
                            break;
                            case Constants.SECTION_HOME: {
                                realURI = getHomeUri(nodes, requestURI, mobile);
                            }
                            break;
                            case Constants.SECTION_INDICATORS: {
                                realURI = getIndicators2Uri(nodes, requestURI, mobile);
                            }
                            break;
                            case Constants.SECTION_INDUSTRIES: {
                                realURI = getIndustriesUri(nodes, requestURI);
                            }
                            break;
                            case Constants.SECTION_SEARCH: {
                                realURI = getSearchUri(nodes, requestURI);
                            }
                            break;
                            case Constants.SECTION_TERMS: {
                                realURI = getTermsUri(nodes, requestURI);
                            }
                            break;
                            case Constants.SECTION_TRADE: {
                                if (httpRequest.getParameterMap().containsKey(Parameters.POSTFIX_TRADE_SUNBURST_DATA)) {
                                    realURI = getTradeSunburstUri(nodes);
                                } else if (httpRequest.getParameterMap().containsKey(Parameters.POSTFIX_TRADE_TOP_PARTNERS_DATA)) {
                                    realURI = getTradeTopPartnersUri(nodes);
                                } else {
                                    realURI = getTradeUri(nodes, requestURI);
                                }
                            }
                            break;
                            case Constants.SECTION_TRADE_JS: {
                                realURI = getTradeJsUri(nodes);
                            }
                            break;
                            case Constants.SECTION_TRADE_SUMMARY: {
                                realURI = getTradeSummaryUri(nodes, requestURI);
                            }
                            break;
                            case Constants.SECTION_TRADE_TABLE: {
                                realURI = getTradeTable2Uri(nodes);
                            }
                            break;
                            case Constants.SECTION_TREE: {
                                realURI = getTree2Uri(nodes);
                            }
                            break;
                            default:
                                realURI = getErrorUri(nodes);
                        }
                    } else {
                        realURI = getHomeUri(nodes, requestURI, mobile);
                    }
                }
                break;
                default: {
                    realURI = getErrorUri(nodes);
                }
            }
        } else {
            realURI = getHomeUri(nodes, requestURI, mobile);
        }

        if (realURI == null) {
            //in this case the filter should be transparent
            chain.doFilter(request, response);
        } else {
            //forwarding request to the real URI:
            try {
                RequestDispatcher rd = request.getRequestDispatcher(realURI);
                rd.forward(request, response);
            } catch (Exception e) {
                Logger.getLogger(UrlRewriteFilter.class.getName()).log(Level.SEVERE, e.getMessage());
                Logger.getLogger(UrlRewriteFilter.class.getName()).log(Level.SEVERE, "Redirect to 404 from ".concat(realURI));
                System.out.println(e.getMessage());
                realURI = getErrorUri(nodes);
                RequestDispatcher rd = request.getRequestDispatcher(realURI);
                rd.forward(request, response);
            }
        }
    }

    private String getHomeUriMobile(String[] nodes, boolean mobile) {
        StringBuilder buf = new StringBuilder();
        buf.append(Constants.PAGE_MOBILE_HOME);
        if (nodes.length > SECTION_LOCALE) {
            buf.append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND);
        }
        buf.append(Parameters.MOBILE).append(SqlUtils.EQUALS).append(SqlUtils.ONE).append(SqlUtils.AMPERSAND);
        buf.append(Parameters.SECTION).append(SqlUtils.EQUALS).append(Constants.SECTION_HOME).append(SqlUtils.AMPERSAND);
        return buf.toString();
    }

    private String getErrorUri(String[] nodes) {
        StringBuilder buf = new StringBuilder();
        buf.append(Constants.PAGE_ERROR);
        if (nodes.length > SECTION_LOCALE) {
            buf.append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND);
        }
        buf.append(Parameters.SECTION).append(SqlUtils.EQUALS).append(Constants.SECTION_ERROR).append(SqlUtils.AMPERSAND);
        return buf.toString();
    }

    private String getSearchUri(String[] nodes, String requestURI) {
        StringBuilder buf = new StringBuilder();
        buf.append(Constants.PAGE_SEARCH)
                .append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_LOCALE) {
            buf.append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND);
        }
        buf.append(Parameters.SECTION).append(SqlUtils.EQUALS).append(Constants.SECTION_SEARCH).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_SEARCH_FILTER_TEXT) {
            buf.append(Parameters.FILTER_TEXT).append(SqlUtils.EQUALS).append(nodes[SECTION_SEARCH_FILTER_TEXT]).append(SqlUtils.AMPERSAND);
        }
        if (nodes.length > SECTION_SEARCH_SECTION) {
            buf.append(Parameters.SEARCH_SECTIONS).append(SqlUtils.EQUALS).append(nodes[SECTION_SEARCH_SECTION]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getHomeUri(String[] nodes, String requestURI, boolean mobile) {
        StringBuilder buf = new StringBuilder();

        if (mobile) {
            buf
                    .append(Constants.PAGE_MOBILE_HOME)
                    .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(SqlUtils.ONE).append(SqlUtils.AMPERSAND);
        } else {
            buf.append(Constants.PAGE_HOME);
        }
        buf.append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_LOCALE) {
            buf.append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND);
        }
        buf.append(Parameters.SECTION).append(SqlUtils.EQUALS).append(Constants.SECTION_HOME).append(SqlUtils.AMPERSAND);
        return buf.toString();
    }

    private String getTermsUri(String[] nodes, String requestURI) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(Constants.PAGE_TERMS)
                .append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(Constants.SECTION_TERMS).append(SqlUtils.AMPERSAND);
        return buf.toString();
    }

    private String getAboutUri(String[] nodes) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(Constants.PAGE_ABOUT)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(Constants.SECTION_ABOUT).append(SqlUtils.AMPERSAND);
        return buf.toString();
    }

    private String getTradeSunburstUri(String[] nodes) {
        StringBuilder buf = new StringBuilder();

        buf
                .append(SqlUtils.SLASH)
                .append(Constants.SERVLET_TRADE_SUNBURST_DATA)
                .append(SqlUtils.QUESTION)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND)
                .append(Parameters.REPORTER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_REPORTER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.TRADE_FLOW).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_FLOW]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PARTNER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PARTNER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PERIOD).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PERIOD]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY_GROUP_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY_GROUP_TYPE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY]);

        return buf.toString();
    }

    private String getTradeTopPartnersUri(String[] nodes) {
        StringBuilder buf = new StringBuilder();

        buf
                .append(SqlUtils.SLASH)
                .append(Constants.PAGE_TRADE_TOPN_PARTNERS)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.REPORTER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_REPORTER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.TRADE_FLOW).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_FLOW]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PARTNER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PARTNER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PERIOD).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PERIOD]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_TRADE_PARTNER_RANK) {
            buf
                    .append(Parameters.PARTNER_RANK).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PARTNER_RANK]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getTradeJsUri(String[] nodes) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(SqlUtils.SLASH)
                .append(Constants.SERVLET_DYNAMIC_JS)
                .append(SqlUtils.QUESTION)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND)
                .append(Parameters.REPORTER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_REPORTER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.TRADE_FLOW).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_FLOW]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PARTNER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PARTNER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PERIOD).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PERIOD]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY_GROUP_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY_GROUP_TYPE]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_TRADE_COMMODITY) {
            buf
                    .append(Parameters.COMMODITY).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getTradeUri(String[] nodes, String requestURI) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(Constants.PAGE_TRADE)
                .append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND)
                .append(Parameters.REPORTER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_REPORTER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.TRADE_FLOW).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_FLOW]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PARTNER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PARTNER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PERIOD).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PERIOD]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY_GROUP_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY_GROUP_TYPE]).append(SqlUtils.AMPERSAND);

        if (nodes.length > SECTION_TRADE_COMMODITY) {
            buf
                    .append(Parameters.COMMODITY).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_COMMODITY]).append(SqlUtils.AMPERSAND);
        }
        if (nodes.length > SECTION_TRADE_PARTNER_RANK) {
            buf
                    .append(Parameters.PARTNER_RANK).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_PARTNER_RANK]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getTradeSummaryUri(String[] nodes, String requestURI) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(Constants.PAGE_TRADE_SUMMARY)
                .append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_TRADE_SUMMARY_CLASSIFICATION) {
            buf
                    .append(Parameters.COMMODITY_GROUP_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_SUMMARY_CLASSIFICATION]).append(SqlUtils.AMPERSAND);
        }
        if (nodes.length > SECTION_TRADE_SUMMARY_REPORTER) {
            buf
                    .append(Parameters.REPORTER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_SUMMARY_REPORTER]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getIndicators2Uri(String[] nodes, String requestURI, boolean mobile) {
        StringBuilder buf = new StringBuilder();
        if (mobile) {
            buf
                    .append(Constants.PAGE_MOBILE_INDICATOR)
                    .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(SqlUtils.ONE).append(SqlUtils.AMPERSAND);
        } else {
            buf.append(Constants.PAGE_INDICATOR);
        }
        buf
                .append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_INDICATOR_INDICATOR) {
            buf.append(Parameters.INDICATOR).append(SqlUtils.EQUALS).append(nodes[SECTION_INDICATOR_INDICATOR]).append(SqlUtils.AMPERSAND);
        }
        if (nodes.length > SECTION_INDICATOR_COUNTRY) {
            buf.append(Parameters.COUNTRY).append(SqlUtils.EQUALS).append(nodes[SECTION_INDICATOR_COUNTRY]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getIndustriesUri(String[] nodes, String requestURI) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(Constants.PAGE_INDUSTRIES)
                .append(Parameters.REQUEST_URI).append(SqlUtils.EQUALS).append(requestURI).append(SqlUtils.AMPERSAND)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_INDUSTRIES_INDICATOR) {
            buf.append(Parameters.INDICATOR).append(SqlUtils.EQUALS).append(nodes[SECTION_INDUSTRIES_INDICATOR]).append(SqlUtils.AMPERSAND);
        }
        if (nodes.length > SECTION_INDUSTRIES_COUNTRY) {
            buf.append(Parameters.COUNTRY).append(SqlUtils.EQUALS).append(nodes[SECTION_INDUSTRIES_COUNTRY]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getTree2Uri(String[] nodes) {
        StringBuilder buf = new StringBuilder();

        String page = nodes[SECTION_TREE2_TEMPLATE_TYPE];
        switch (page.toLowerCase()) {
            case Constants.TREE_TEMPLATE_NO_CODES:
                page = Constants.PAGE_TREE_NO_CODES;
                break;
            case Constants.TREE_TEMPLATE_NO_SPACES:
                page = Constants.PAGE_TREE_NO_SPACES;
                break;
            case Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES:
                page = Constants.PAGE_TREE_NO_SPACES_NO_CODES;
                break;
            case Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1:
                page = Constants.PAGE_TREE_NO_SPACES_NO_CODES_BOLD1;
                break;
            case Constants.TREE_TEMPLATE_FULL:
                page = Constants.PAGE_TREE;
                break;
            case Constants.TREE_TEMPLATE_NO_CODES_BOLDGROUP:
                page = Constants.PAGE_TREE_NO_CODES_BOLDGROUP;
                break;
            default:
                page = Constants.PAGE_TREE;
        }

        buf
                .append(page)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND)
                .append(Parameters.FILTER_REQUEST_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TREE2_REQUEST_TYPE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.FILTER_TEMPLATE_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TREE2_TEMPLATE_TYPE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.URL_TEMPLATE).append(SqlUtils.EQUALS).append(nodes[SECTION_TREE2_URL_TEMPLATE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.FILTER_CODE).append(SqlUtils.EQUALS).append(nodes[SECTION_TREE2_FILTER_CODE]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_TREE2_SELECTED_IDS) {
            buf.append(Parameters.SELECTED_IDS).append(SqlUtils.EQUALS).append(nodes[SECTION_TREE2_SELECTED_IDS]).append(SqlUtils.AMPERSAND);
        }
        if (nodes.length > SECTION_TREE2_FILTER_TEXT) {
            buf.append(Parameters.FILTER_TEXT).append(SqlUtils.EQUALS).append(nodes[SECTION_TREE2_FILTER_TEXT]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    private String getTradeTable2Uri(String[] nodes) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(Constants.PAGE_TRADE_TABLE)
                .append(Parameters.LOCALE).append(SqlUtils.EQUALS).append(nodes[SECTION_LOCALE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.SECTION).append(SqlUtils.EQUALS).append(nodes[SECTION_SECTION]).append(SqlUtils.AMPERSAND)
                .append(Parameters.REPORTER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_REPORTER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.TRADE_FLOW).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_FLOW]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PARTNER).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_PARTNER]).append(SqlUtils.AMPERSAND)
                .append(Parameters.PERIOD).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_PERIOD]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY_GROUP_TYPE).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_COMMODITY_GROUP_TYPE]).append(SqlUtils.AMPERSAND)
                .append(Parameters.COMMODITY).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_COMMODITY]).append(SqlUtils.AMPERSAND);
        if (nodes.length > SECTION_TRADE_TABLE_LOAD_TABLE_DATA) {
            buf.append(Parameters.LOAD_TABLE_DATA).append(SqlUtils.EQUALS).append(nodes[SECTION_TRADE_TABLE_LOAD_TABLE_DATA]).append(SqlUtils.AMPERSAND);
        }
        return buf.toString();
    }

    @Override
    public void destroy() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
